package com.yiwei.zxp.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.text.Spanned;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.allen.library.SuperButton;
import com.bumptech.glide.Glide;
import com.yiwei.zxp.Application;
import com.yiwei.zxp.R;
import com.yiwei.zxp.activity.BankPayMoneyActivity;
import com.yiwei.zxp.activity.MyOrderDetailActivity;
import com.yiwei.zxp.activity.UlinePayFinishActivity;
import com.yiwei.zxp.activity.WechatChatActivity;
import com.yiwei.zxp.api.model.MyOrderListModel;
import com.yiwei.zxp.constant.Const;
import com.yiwei.zxp.util.Arith;
import com.yiwei.zxp.util.Dialog;
import com.yiwei.zxp.util.PreferenceUtils;
import com.yiwei.zxp.util.Spans;
import com.yiwei.zxp.util.UI;
import com.yiwei.zxp.util.Util;
import com.yiwei.zxp.view.PacketPayFinishDilaog;
import com.yiwei.zxp.view.PacketPayStepDilaog;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * Created by xsooy-pc on 2017/10/23.
 */

public class MyOrderAdapter extends ItemAdapter<MyOrderListModel.DatasBean, MyOrderAdapter.DefaultHolder> {
    private final int textSize36;
    private final int textSize24;


    private Context context;

    public MyOrderAdapter(Context context) {
        super(context);
        textSize36 = context.getResources().getDimensionPixelSize(R.dimen.x36);
        textSize24 = context.getResources().getDimensionPixelSize(R.dimen.x24);

        this.context = context;
    }

    @Override
    public DefaultHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new DefaultHolder(inflate(R.layout.item_my_order, parent));
    }

    @Override
    public void onBindViewHolder(DefaultHolder holder, int position) {
        MyOrderListModel.DatasBean item = getItem(position);
        holder.item = item;
        holder.txtTitle.setText(item.combo_name);
//        holder.txtContent.setText(item.combo_desc);
        holder.txtContent.setText(item.combo_num + "个商品展示位");
        holder.txtMoney.setText("￥" + (Double.valueOf(item.combo_single_money) * Double.valueOf(item.combo_num)));
        holder.txtNum.setText("x" + item.buy_num);
        holder.txtCardType.setText(item.order_number);

        if (item.pay_way.equals("1")) {
            holder.txtStatus.setText(getStatus(item.status) + "(线下支付)");
        } else if (item.pay_way.equals("2")) {
            holder.txtStatus.setText(getStatus(item.status) + "(银行卡支付)");
        }


        Glide.with(context).load(item.combo_image).into(holder.imgCardType);
        Spanned money = Spans.with(context).font("购买套餐数量：" + item.buy_num + "   合计：", textSize24, 0xff000000).
                font("  ￥", textSize24, 0xffFF0030).
                font(item.money, textSize36, 0xffFF0030).
                build();
        holder.txtInfo.setText(money);
        if (item.status.equals("0")) {

            if (item.pay_way.equals("1")) {
//            线下支付
                holder.btnOne.setVisibility(View.VISIBLE);
                holder.btnTwo.setVisibility(View.VISIBLE);
                holder.btnTwo.setText("支付流程");
                holder.btnThree.setVisibility(View.VISIBLE);
                holder.btnThree.setText("完成支付");
            } else if (item.pay_way.equals("2")) {
//银行卡支付
                holder.btnOne.setVisibility(View.GONE);
                holder.btnTwo.setVisibility(View.GONE);
                holder.btnThree.setVisibility(View.VISIBLE);
                holder.btnThree.setText("付款");
            }


        } else if (item.status.equals("3")) {
            holder.btnOne.setVisibility(View.GONE);
            holder.btnTwo.setVisibility(View.VISIBLE);
            holder.btnTwo.setText("删除订单");
            holder.btnThree.setVisibility(View.VISIBLE);
            holder.btnThree.setText("重新购买");
        } else if (item.status.equals("7") || item.status.equals("1")) {
            holder.btnOne.setVisibility(View.GONE);
            holder.btnTwo.setVisibility(View.VISIBLE);
            holder.btnTwo.setText("删除订单");
            holder.btnThree.setVisibility(View.VISIBLE);
            holder.btnThree.setText("再次购买");
        } else if (item.status.equals("4")) {
            if (item.pay_way.equals("1")) {
                holder.txtStatus.setText(getStatus(item.status) + "(线下支付)");
                holder.btnOne.setVisibility(View.VISIBLE);
                holder.btnTwo.setVisibility(View.GONE);
                holder.btnThree.setVisibility(View.VISIBLE);
                holder.btnThree.setText("修改凭证");
            }
        } else if (item.status.equals("5")) {
            if (item.pay_way.equals("1")) {
                holder.txtStatus.setText(getStatus(item.status) + "(线下支付)");
                holder.btnOne.setVisibility(View.VISIBLE);
                holder.btnTwo.setVisibility(View.GONE);
                holder.btnThree.setVisibility(View.VISIBLE);
                holder.btnThree.setText("修改凭证");
            }
        } else {
            holder.btnOne.setVisibility(View.GONE);
            holder.btnTwo.setVisibility(View.GONE);
            holder.btnThree.setVisibility(View.GONE);
        }
    }

    //状态（订单状态：0 -待支付、3-订单失效，4：审核中，5：审核失败，7：已完成）
    private String getStatus(String status) {
        switch (status) {
            case "0":
                return "待付款";
            case "1":
                return "已支付";
            case "2":
                return "支付失败";
            case "3":
                return "订单失效";
            case "4":
                return "审核中";
            case "5":
                return "审核失败";
            case "6":
                return "";
            case "7":
                return "已完成";
            default:
                return "";
        }
    }


    class DefaultHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.btn_one)
        SuperButton btnOne;
        @BindView(R.id.btn_two)
        SuperButton btnTwo;
        @BindView(R.id.btn_three)
        SuperButton btnThree;
        @BindView(R.id.txt_card_type)
        TextView txtCardType;
        @BindView(R.id.txt_status)
        TextView txtStatus;
        @BindView(R.id.img_card_type)
        ImageView imgCardType;
        @BindView(R.id.fl_card_type)
        FrameLayout flCardType;
        @BindView(R.id.txt_title)
        TextView txtTitle;
        @BindView(R.id.txt_content)
        TextView txtContent;
        @BindView(R.id.txt_money)
        TextView txtMoney;
        @BindView(R.id.txt_num)
        TextView txtNum;
        @BindView(R.id.txt_info)
        TextView txtInfo;
        @BindView(R.id.layout_total)
        LinearLayout layoutTotal;
        MyOrderListModel.DatasBean item;

        @OnClick({R.id.btn_two, R.id.btn_one, R.id.btn_three, R.id.layout_total})
        public void onViewClicked(View view) {
            switch (view.getId()) {
                case R.id.btn_delete:

                    break;
                case R.id.btn_one:
//                    Dialog.showDefaultDialog(context, "", "请确认拨打云淘在线官方电话", "取消", "呼叫", false, new Dialog.DialogDefaultClickListener() {
//                        @Override
//                        public void confirm() {
//                            Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + PreferenceUtils.getString(Application.getInstance(), "service_phone", "")));
//                            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                            context.startActivity(intent);
//                        }
//
//                        @Override
//                        public void cancel() {
//
//                        }
//                    });
                    Util.startActivity(context, WechatChatActivity.class);
                    break;

                case R.id.btn_two:
                    if (UI.toString(btnTwo).equals("支付流程")) {
                        new PacketPayStepDilaog(context).setOnChooseListener(new PacketPayStepDilaog.OnChooseListener() {
                            @Override
                            public void onSubmit() {

                            }
                        }).show();
                    } else if (UI.toString(btnTwo).equals("删除订单")) {
                        if (onChooseListener != null) {
                            onChooseListener.onDelete(item.money, item.order_number);
                        }
                    }
                    break;
                case R.id.btn_three:
                    if (UI.toString(btnThree).equals("完成支付")) {
//                        new PacketPayFinishDilaog(context).setOnChooseListener(new PacketPayFinishDilaog.OnChooseListener() {
//                            @Override
//                            public void onEditFinish(String count) {
//                                if (onChooseListener != null) {
//                                    onChooseListener.onPayFinish(item.order_number, count);
//                                }
//                            }
//                        }).show();
                        if (onChooseListener != null) {
                            onChooseListener.onPayFinish(item.order_number);
                        }
                    } else if (UI.toString(btnThree).equals("修改凭证")) {
                        Intent intent = new Intent(context, UlinePayFinishActivity.class);
                        intent.putExtra("order_number", item.order_number);
                        intent.putExtra("is_has_upload", true);
                        context.startActivity(intent);
                        ((Activity) context).overridePendingTransition(R.anim.right_enter, R.anim.left_out);
                    } else if (UI.toString(btnThree).equals("再次购买") || UI.toString(btnThree).equals("重新购买")) {
                        if (onChooseListener != null) {
                            onChooseListener.onBuyMoreTime();
                        }
                    } else if (UI.toString(btnThree).equals("付款")) {
                        ArrayList<String> orderData = new ArrayList<String>();
                        orderData.add(item.order_number);
                        Intent intent = new Intent(context, BankPayMoneyActivity.class);
                        intent.putStringArrayListExtra("data", orderData);
                        intent.putExtra("need_money", Double.valueOf(item.money));
                        intent.putExtra("pay_type", Const.PayType.BOOTH_ORDER);
                        context.startActivity(intent);
                    }
                    break;
                case R.id.layout_total:
                    Intent intent = new Intent(context, MyOrderDetailActivity.class);
                    intent.putExtra("order_number", item.order_number);
                    if (UI.toString(txtStatus).equals("审核中(线下支付)") || UI.toString(txtStatus).equals("审核失败(线下支付)")) {
                        intent.putExtra("is_uline", true);
                    }
                    context.startActivity(intent);
                    ((Activity) context).overridePendingTransition(R.anim.right_enter, R.anim.left_out);
                    break;
            }
        }

        public DefaultHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    private OnChooseListener onChooseListener;

    public void setOnChooseListener(OnChooseListener onChooseListener) {
        this.onChooseListener = onChooseListener;
    }

    public interface OnChooseListener {
        void onClick(String money, String order_no);

        void onDelete(String money, String order_no);

        void onBuyMoreTime();

        void onPayFinish(String order_no);


    }
}
